package mandelbrot.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import mandelbrot.core.MandelCalculator;
import mandelbrot.core.Worker;


/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class MainFrame extends javax.swing.JFrame implements ActionListener {
	private MandelCanvas drawingCanvas;
	private JPanel settingPanel;
	private JSpinner xSpinner;
	private JLabel yLabel;
	private JSpinner ySpinner;
	private JButton refreshButton;
	private JLabel XLabel;
	
	private int clientID;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame inst = new MainFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public MainFrame() {
		super();
		int tmp = (int) (Math.random() * 100);
		clientID = tmp;
		initGUI();
	}

	private void initGUI() {
		try {
			try {
				// Set cross-platform Java L&F (also called "Metal")
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			} catch (UnsupportedLookAndFeelException e) {
				// handle exception
			} catch (ClassNotFoundException e) {
				// handle exception
			} catch (InstantiationException e) {
				// handle exception
			} catch (IllegalAccessException e) {
				// handle exception
			}
			this.setTitle("Client"+clientID);
			GroupLayout thisLayout = new GroupLayout((JComponent) getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			{
				drawingCanvas = new MandelCanvas();
			}
			{
				settingPanel = new JPanel();
				GroupLayout settingPanelLayout = new GroupLayout((JComponent) settingPanel);
				settingPanel.setLayout(settingPanelLayout);
				{
					// SpinnerListModel xSpinnerModel =
					// new SpinnerListModel(
					// new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" ,
					// "Fri" , "Sat" });
					SpinnerModel xModel = new SpinnerNumberModel(1, 1, 100, 1);
					xSpinner = new JSpinner(xModel);
					// xSpinner.setModel(xSpinnerModel);
				}
				{
					XLabel = new JLabel();
					XLabel.setText("Horizontal Divisions: ");
				}
				{
					yLabel = new JLabel();
					yLabel.setText("Vertical Divisions: ");
				}
				{
					refreshButton = new JButton();
					refreshButton.setText("Refresh");
					refreshButton.setActionCommand("REFRESH");
					refreshButton.addActionListener(this);
				}
				{
					// SpinnerListModel ySpinnerModel =
					// new SpinnerListModel(
					// new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" ,
					// "Fri" , "Sat" });
					SpinnerModel yModel = new SpinnerNumberModel(1, 1, 100, 1);
					ySpinner = new JSpinner(yModel);

					// ySpinner.setModel(ySpinnerModel);
				}
				settingPanelLayout.setHorizontalGroup(settingPanelLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(settingPanelLayout.createParallelGroup()
					    .addComponent(XLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
					    .addComponent(yLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE))
					.addGap(29)
					.addGroup(settingPanelLayout.createParallelGroup()
					    .addComponent(xSpinner, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
					    .addComponent(ySpinner, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE))
					.addGap(0, 76, Short.MAX_VALUE)
					.addComponent(refreshButton, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
					.addContainerGap());
				settingPanelLayout.setVerticalGroup(settingPanelLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(settingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(xSpinner, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(XLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(settingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(ySpinner, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(yLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(0, 48, Short.MAX_VALUE)
					.addComponent(refreshButton, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
					.addContainerGap());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup().addContainerGap().addComponent(drawingCanvas, 0, 320, Short.MAX_VALUE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(settingPanel, 0, 205, Short.MAX_VALUE).addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout.createParallelGroup().addComponent(drawingCanvas, GroupLayout.Alignment.LEADING, 0, 451, Short.MAX_VALUE)
									.addComponent(settingPanel, GroupLayout.Alignment.LEADING, 0, 451, Short.MAX_VALUE)).addContainerGap());
			pack();
			this.setSize(491, 464);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "REFRESH") {
			
			
			MandelCalculator calculator = new MandelCalculator(drawingCanvas, clientID, -2.1, -1.25, 1.1, 1.25, 1000, (Integer)xSpinner.getValue(), (Integer) ySpinner.getValue());

			Thread thread = new Thread(calculator);
			thread.start();
		}

	}

}
